﻿Shader "Unlit/NewUnlitShader"
{
    Properties
    {
        xPixels("xPixels", 2D) = "white" {}
        xPalette("xPalette", 2D) = "white" {}
    }

CGINCLUDE

#include "UnityCG.cginc"

#define SAMPLE_TEXTURE(Name, texCoord)  tex2D(Name, texCoord)



        struct appdata
    {
        float4 vertex : POSITION;
        float2 uv : TEXCOORD0;
        float4 Color: COLOR0;
    };

    struct v2f
    {
        float2 uv : TEXCOORD0;
        UNITY_FOG_COORDS(1)
            float4 vertex : SV_POSITION;
        float4 Color: COLOR0;
    };

    sampler2D xPixels;
    sampler2D xPalette;
    float4 xPixels_ST;

    float4x4 xMatrix;
    float4x4 xRotation;

    float xBlendWeight;

    int xPalFx_Use;
    float4 xPalFx_Add;
    float4 xPalFx_Mul;
    int xPalFx_Invert;
    float xPalFx_Color;
    float4 xPalFx_SinMath;

    int xAI_Use;
    int xAI_Invert;
    float xAI_color;
    float4 xAI_preadd;
    float4 xAI_contrast;
    float4 xAI_postadd;
    float4 xAI_paladd;
    float4 xAI_palmul;
    int xAI_number;

    int xFontColorIndex;
    int xFontTotalColors;

    float4 BaseColor(float4 inputcolor, float base)
    {
        if (base == 0.0f)
        {
            float color = (0.299f * inputcolor.r) + (0.587f * inputcolor.g) + (0.114f * inputcolor.b);
            return float4(color, color, color, inputcolor.a);
        }
        return float4(inputcolor.rgb * base, inputcolor.a);
    }

    float4 PalFx(float4 inputcolor)
    {
        float4 output = BaseColor(inputcolor, xPalFx_Color);

        if (xPalFx_Invert == 1) output = float4(1 - output.rgba);

        output = (output + xPalFx_Add + xPalFx_SinMath) * xPalFx_Mul;

        return float4(output.bgr, inputcolor.a);
    }

    float4 AfterImage(float4 inputcolor)
    {
        float4 output = BaseColor(inputcolor, xAI_color);

        if (xAI_Invert == 1) output = float4(1 - output.rgba);

        output += xAI_preadd;
        output *= xAI_contrast;
        output += xAI_postadd;

        [unroll(25)]
        for (int i = 0; i <= xAI_number; ++i) output = (output + xAI_paladd) * xAI_palmul;

        return float4(output.bgr, inputcolor.a);
    }

    v2f vert(appdata v)
    {
        v2f o;
        //o.vertex = UnityObjectToClipPos(v.vertex);
        o.vertex = mul(mul(v.vertex, xMatrix), xRotation);
        //o.uv = TRANSFORM_TEX(v.uv, xPixels);
        o.uv = v.uv;
        o.Color = v.Color;
        //UNITY_TRANSFER_FOG(o,o.vertex);
        return o;
    }

    fixed4 frag(v2f i) : SV_Target
    {
         float color_index = SAMPLE_TEXTURE(xPixels, i.uv).r;
         if (color_index == 0.0f) return float4(0, 0, 0, 0);

         float4 output_color = SAMPLE_TEXTURE(xPalette, color_index);

          if (xPalFx_Use == 1) output_color = PalFx(output_color);
          if (xAI_Use == 1) output_color = AfterImage(output_color);

           output_color *= i.Color;

           return float4(output_color.bgr, xBlendWeight);
    }

        fixed4 FontPixelShader(v2f i) : SV_Target
    {

            float color_index = SAMPLE_TEXTURE(xPixels, i.uv).r;
            if (color_index == 0.0f) return float4(0, 0, 0, 0);

            float per = 1.0 - float(xFontColorIndex) / float(xFontTotalColors);
            float4 output_color = SAMPLE_TEXTURE(xPalette, color_index * per - 1.0 / 255.0);

            output_color *= i.Color;

            return float4(output_color.bgr, xBlendWeight);
    }

        ENDCG

    SubShader
    {
        Tags { "RenderType" = "Opaque" }
        //ZWrite Off // 关闭深度写入
        //Blend SrcAlpha OneMinusSrcAlpha // 混合的参数
        LOD 100

        Pass
        {
             Name "Draw"
             ZTest Always Cull Off ZWrite Off
            Fog{ Mode off }

            CGPROGRAM
            #pragma glsl
            #pragma fragmentoption ARB_precision_hint_fastest
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0
            ENDCG
        }

        Pass
        {
             Name "FontDraw"
             ZTest Always Cull Off ZWrite Off
            Fog{ Mode off }

            CGPROGRAM
            #pragma glsl
            #pragma fragmentoption ARB_precision_hint_fastest
            #pragma vertex vert
            #pragma fragment FontPixelShader
            #pragma target 3.0
            ENDCG
        }
    }
    FallBack "Diffuse"
}
